function $key(getkeys) {
    return document.querySelector(getkeys);
}
var top = document.getElementsByClassName("top")[0];
var nav = document.getElementsByClassName("nav")[0];
// 提取日期相关函数
date();
function date() {
    var spa = document.querySelector(".dater");
    var timer = null;
    // 封装时间日期格式化
    function mydate() {
        var date = new Date();
        // 时间日期格式化 yyyy-mm-dd hh:mm:ss
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var data = date.getDate();
        var hours = date.getHours();
        var minutes = date.getMinutes();
        var seconds = date.getSeconds();
        // 补 0
        month = (month < 10) ? "0" + month : month;
        data = (data < 10) ? "0" + data : data;
        hours = (hours < 10) ? "0" + hours : hours;
        minutes = (minutes < 10) ? "0" + minutes : minutes;
        seconds = (seconds < 10) ? "0" + seconds : seconds;
        var res = `${year}年${month}月${data}日 ${hours}:${minutes}:${seconds}`;
        return res;
    }

    function automove() {
        spa.innerHTML = mydate();
    }
    // 自动走
    setInterval(automove, 1000);
}
var username = sessionStorage.getItem('userAccount');
document.querySelector('.namer').textContent = username;

var projectChildId = Number(localStorage.getItem("zzz"));
var projectId = Number(localStorage.getItem("projectId"));

//添加任务
var Img = document.querySelector(".Img");
Img.onclick = function () {
    var Masks = document.createElement("div");
    Masks.className = "Masks";
    $key(".nav_content").insertBefore(Masks, $key(".nav_content").firstElementChild);
    var Modal = document.createElement("div");
    Modal.className = "Modal";
    Modal.innerHTML = `
              <div class="wtp">添加任务</div> 
              <div class="wto">
                  <div>项目名称：<input class="cs1" type="text"></div>
                      <div>负责人：<select class="charg"> </select></div>
                  <div>项目工期：<input  class="cs2" type="text"></div>
                  <div>开始时间：<input class="cs3" type="date"></div>
                      <div>结束时间：<input class="cs4" type="date"></div>
                      <div>比重：<input  class="cs5" type="text"></div>
              </div>
              <div class="wtf">
                  <button class="Confirra">确认</button>
                  <button class="Cance">取消</button>
              </div>
              `
    $key(".nav_content").insertBefore(Modal, $key(".nav_content").firstElementChild.nextSibling);
    var charg = document.getElementsByClassName("charg")[0];
    var charg = document.querySelector(".charg");
    ajax.post("http://192.168.199.101:8080/BaorSystem/projector/addProjectUserList", {}, function (res) {
        if (res.code == 200) {
            var dataset = res.dataset;
            for (var i = 0; i < dataset.length; i++) {
                var newoption = document.createElement("option");
                newoption.innerHTML = `${dataset[i].userName}`;
                newoption.value = `${dataset[i].userId}`
                charg.appendChild(newoption);
            }

        }
    })
    Cancel();
    fonfga();
}

//添加任务-确认
function fonfga() {
    var Masks = document.querySelector(".Masks");
    var Modal = document.querySelector(".Modal");
    var Confirra = document.getElementsByClassName("Confirra")[0];
    Confirra.onclick = function () {
        var cs1 = document.getElementsByClassName("cs1")[0];
        var cs2 = document.getElementsByClassName("cs2")[0];
        var cs3 = document.getElementsByClassName("cs3")[0];
        var cs4 = document.getElementsByClassName("cs4")[0];
        var cs5 = document.getElementsByClassName("cs5")[0];
        var charg = document.querySelector(".charg");
        var slt = {
            projectChildId: projectChildId,
            taskName: cs1.value,
            taskLeader: Number(charg.value),
            workingHours: cs2.value,
            startDate: cs3.value,
            endDate: cs4.value,
            projectId: projectId,
            progress: 20,
            prop: cs5.value,
        }
        ajax.post("http://192.168.199.101:8080/BaorSystem/task/insertTask", slt, function (sur) {
            if (sur.code == 200) {
                console.log(sur);
            }
        })
        $key(".nav_content").removeChild(Masks);
        $key(".nav_content").removeChild(Modal);
    }
}
// 子项目列表
var pageIndex = 1;
var pageSize = 8;
var Tbody = document.querySelector(".Tbody");
console.log(projectChildId);
function mov() {
    Tbody.innerHTML = "";
    var res = {
        projectChildId: projectChildId,
        pageIndex: pageIndex,
        pageSize: pageSize,
    }
    ajax.post("http://192.168.199.101:8080/BaorSystem/task/taskList", res, function (res) {
        if (res.code == 200) {
            console.log(res);
            taskList = res.dataset.taskList;
            for (var i in taskList) {
                var newTr = document.createElement("tr");
                newTr.innerHTML = `
                          <td>${taskList[i].taskId}</td>
                          <td>${taskList[i].taskName}</td>
                          <td>${taskList[i].taskLeader}</td>
                          <td>${taskList[i].workingHours}</td>
                          <td>${taskList[i].startDate}</td>
                          <td>${taskList[i].endDate}</td>
                          <td>${taskList[i].progress}</td>
                          <td>${taskList[i].prop}</td>
                          <td><button class="revis">修改</button><button class="detai">删除</button></td>
                      `;
                Tbody.appendChild(newTr);
            }
            revise();
            detail();
            pageCount = res.dataset.pageCount;
            var fot = document.querySelector(".fot");
            fot.innerHTML = "";
            for (var i = 1; i <= pageCount; i++) {
                var But = document.createElement("button");
                But.innerText = i;
                But.className = "page";
                fot.appendChild(But);
                Page();
            }
        }
    })

}
mov();

// 总班级上一页
var first = document.querySelector(".first");
var last = document.querySelector(".last");
first.onclick = function () {
    pageIndex--;
    if (pageIndex <= 1) {
        pageIndex = 1;
        this.disabled = true;
    }
    last.disabled = false;
    mov();
}

// 总班级下一页
last.onclick = function () {
    pageIndex++;
    if (pageIndex >= pageCount) {
        pageIndex = pageCount;
        this.disabled = true;
    }
    first.disabled = false;
    mov();
    // sou();
}

//页码点击
function Page() {
    var page = document.getElementsByClassName("page");
    for (var j = 0; j < page.length; j++) {
        page[j].onclick = function () {
            for (var i = 0; i < page.length; i++) {
                if (this == page[i]) {
                    pageIndex = i + 1;
                    console.log(page[i]);
                    page[i].style.backgroundColor = "blue";
                    page[i].style.color = "#fff";
                    mov();
                } else {
                    page[i].style.backgroundColor = "#fff";
                    page[i].style.color = "#000";
                }
            }

        }
    }
}

//修改
function revise() {
    var revis = document.getElementsByClassName("revis");
    for (var i = 0; i < revis.length; i++) {
        revis[i].onclick = function () {
            for (var j = 0; j < revis.length; j++) {
                if (this == revis[j]) {
                    taskId = taskList[j].taskId;
                    console.log(taskId);
                    var Masks = document.createElement("div");
                    Masks.className = "Masks";
                    $key(".nav_content").insertBefore(Masks, $key(".nav_content").firstElementChild);
                    var Modal = document.createElement("div");
                    Modal.className = "Modal";
                    Modal.innerHTML = `
                              <div class="wtp">修改任务</div> 
                              <div class="wto">
                                  <div>项目名称：<input class="cs1" type="text"></div>
                                   <div>负责人：<select class="charg"> </select></div>
                                  <div>项目工期：<input  class="cs2" type="text"></div>
                                  <div>开始时间：<input class="cs3" type="date"></div>
                                   <div>结束时间：<input class="cs4" type="date"></div>
                              </div>
                              <div class="wtf">
                                  <button class="Confirr">确认</button>
                                  <button class="Cance">取消</button>
                              </div>
                              `
                    $key(".nav_content").insertBefore(Modal, $key(".nav_content").firstElementChild.nextSibling);
                    // charge();'
                    //班级id
                    var charg = document.getElementsByClassName("charg")[0];
                    var charg = document.querySelector(".charg");
                    ajax.post("http://192.168.199.101:8080/BaorSystem/projector/addProjectUserList", {}, function (res) {
                        if (res.code == 200) {
                            var dataset = res.dataset;
                            for (var i = 0; i < dataset.length; i++) {
                                var newoption = document.createElement("option");
                                newoption.innerHTML = `${dataset[i].userName}`;
                                newoption.value = `${dataset[i].userId}`
                                charg.appendChild(newoption);
                            }

                        }
                    })
                    Cancel();
                    //确认
                    fonfg();
                }
            }
        }
    }
}

//确认
function fonfg() {
    var Masks = document.querySelector(".Masks");
    var Modal = document.querySelector(".Modal");
    var Confirr = document.getElementsByClassName("Confirr")[0];
    Confirr.onclick = function () {
        var cs1 = document.getElementsByClassName("cs1")[0];
        var cs2 = document.getElementsByClassName("cs2")[0];
        var cs3 = document.getElementsByClassName("cs3")[0];
        var cs4 = document.getElementsByClassName("cs4")[0];
        var charg = document.querySelector(".charg");
        var slt = {
            projectChildId: projectChildId,
            taskName: cs1.value,
            taskLeader: Number(charg.value),
            workingHours: cs2.value,
            startDate: cs3.value,
            endDate: cs4.value,
            taskId: taskId,
            projectId: projectChildId,
            progress: 20,
            prop: 20,
        }
        ajax.post("http://192.168.199.101:8080/BaorSystem/task/updateTask", slt, function (sur) {
            if (sur.code == 200) {
                console.log(sur);
            }
        })
        $key(".nav_content").removeChild(Masks);
        $key(".nav_content").removeChild(Modal);
    }
}

// 取消
function Cancel() {
    var Cance = document.getElementsByClassName("Cance")[0];
    var Masks = document.querySelector(".Masks");
    var Modal = document.querySelector(".Modal");
    Cance.addEventListener("click", function () {
        $key(".nav_content").removeChild(Masks);
        $key(".nav_content").removeChild(Modal);
    })
}

//删除
function detail() {
    var detai = document.querySelectorAll(".detai");
    for (var i = 0; i < detai.length; i++) {
        detai[i].onclick = function () {
            for (var j = 0; j < detai.length; j++) {
                if (this == detai[j]) {
                    var taskId = taskList[j].taskId;
                    console.log(taskId);
                    var ufo = {
                        taskId: taskId,
                    }
                    ajax.post(`http://192.168.199.101:8080/BaorSystem/task/deleteTask/${taskId}`, ufo, function (woo) {
                        if (woo.code == 200) {
                            // mov();
                        }
                    })
                }
            }
        }
    }
}


// 添加点击事件
//班级管理
var classs = document.getElementsByClassName("classs");
classs[0].onclick = function () {
    location.href = "./ClassManagement.html";
}
//日常
var routine = document.getElementsByClassName("routine")[0];
routine.onclick = function () {
    location.href = "./daily.html";
}
//项目管理
var project = document.getElementsByClassName("project")[0];
project.onclick = function () {
    location.href = "./projectmanagement.html";
}
//出差记录
var travel = document.querySelector(".travel");
travel.addEventListener('click', function () {
    location.href = "./business.html";
});
//请假记录
var excused = document.querySelector(".excused");
excused.addEventListener('click', function () {
    location.href = "./vacate.html";
});
//考勤记录
var presence = document.querySelector(".presence");
presence.addEventListener('click', function () {
    location.href = "./clock.html";
});
//系统设置
var system = document.getElementsByClassName("system")[0];
system.onclick = function () {
    location.href = "./setting.html";
}